package sharewithus.analyzer.seg;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

import sharewithus.analyzer.dic.DicWord;
import sharewithus.analyzer.utils.MatchRet;


/**
 * 最大长度分词器
 * @author terry
 *
 */
public class MaxLenthSegmenter extends AbstractSegmenter implements
		SUSegmenter {

	@Override
	protected List<SUToken> doSeg(String txt) {
		List<SUToken> tokens = new ArrayList<SUToken>();
		int end = txt.length() - 1;
		Deque<SUToken> deque = new ArrayDeque<SUToken>();
		int start;
		while (end >= 0) {
			MatchRet<DicWord> ret = dic.matchLong(txt, end);
			if (ret == null)
				break;
			if (ret.getData().getTerm() == null) {
				ret.getData().setTerm(ret.getTerm());
			}
			start = end - ret.getData().getTerm().length();
			deque.push(new SUToken(ret.getData().getTerm(), ret.getData().getPosInfs(), start +1,
					end +1  ));
			end = start;
		}
		for(SUToken token : deque){
			tokens.add(token);
		}
		return tokens;
	}
	
	public static void main(String[] args) {
		SUSegmenter seg = new MaxLenthSegmenter();
		String txt[] = { 
				"法新社消息，日本自卫队直升机飞临福岛第一核电站3号和4号堆上方，每架倾倒了超过7吨水来给反应堆降温。"
		};
		for (String s : txt) {
			List<SUToken> ret = seg.segment(s);
			System.out.println("原文:" + s);
			System.out.println("---------------------------------" );
			for (SUToken token : ret) {
				System.out.println(token.getStart() + "-" + token.getEnd() + " : "
						+ token.getTerm() + " : " + token.getTaggedPOS());
			}
			System.out.println();
		}
	}
}
